﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Facebook.Entity;
using Facebook.Components;
using System.Collections.ObjectModel;
using InterOp;


/// <summary>
/// realize the connection to Facebook and imports data about a user
/// </summary>
public class FacebookConnection : ISocialNetwork
{
    private Page m_WebPage;
    private FacebookService _fbService;

    public Page WebPage
    {
        get
        {
            return m_WebPage;
        }

        set
        {
            m_WebPage = value;
        }
    }

    /// <summary>
    /// realize a connection to the Facebook application that handles information about a specified user
    /// </summary>
    /// <param name="Page">current page used for retrieving information about the session</param>
    public FacebookConnection(Page Page)
    {
        m_WebPage = Page;

        _fbService = new FacebookService();

        // ApplicationKey and Secret are acquired when you sign up for 
        _fbService.ApplicationKey = "fbd463f070e95aa124292e21b2b56798";
        _fbService.Secret = "fe02e55d7daf539cc00b50d1bb70bd59";
        _fbService.IsDesktopApplication = false;

        string sessionKey = m_WebPage.Session["Facebook_session_key"] as String;
        string userId = m_WebPage.Session["Facebook_userId"] as String;

        // When the user uses the Facebook login page, the redirect back here 
        // We need the auth_token in the query params
        string authToken = m_WebPage.Request.QueryString["auth_token"];

        // We have already established a session on behalf of this user
        if (!String.IsNullOrEmpty(sessionKey))
        {
            _fbService.SessionKey = sessionKey;
            _fbService.UserId = userId;
        }
        // This will be executed when Facebook login redirects to our page
        else if (!String.IsNullOrEmpty(authToken))
        {
            _fbService.CreateSession(authToken);
            m_WebPage.Session["Facebook_session_key"] = _fbService.SessionKey;
            m_WebPage.Session["Facebook_userId"] = _fbService.UserId;
            m_WebPage.Session["Facebook_session_expires"] = _fbService.SessionExpires;
        }
        // Need to login
        else
        {
            m_WebPage.Response.Redirect(@"http://www.Facebook.com/login.php?api_key=" + _fbService.ApplicationKey + @"&v=1.0\");
        }
    }
    #region ISocialNetwork Members

   
    /// <summary>
    /// import information about the profile of a Facebook user 
    /// </summary>
    /// <param name="username">username of the Facebook user whose information we want to retrieve </param>
    /// <returns>set of infomation about the Facebook user</returns>
    public UserData importProfile(string username)
    {
        Facebook.Entity.User user = _fbService.GetUserInfo();
        UserData data = new UserData();

        data.Name = user.FirstName;
        data.Surname = user.LastName;
        data.Birthday = user.Birthday;
        data.City = user.CurrentLocation.City;
        data.Address = user.CurrentLocation.ToString();
        data.Gender = user.Sex.ToString();
        data.Interest = user.Interests;
        data.Nickname = user.SignificantOtherId;
        data.UserID = user.UserId;

    
        return data;
    }


    /// <summary>
    /// import the friend list of a Facebook user 
    /// </summary>
    /// <param name="username">username of the Facebook user whose friends we want to retrieve </param>
    /// <returns>list of Ids of the Facebook friends of the user</returns>
    public List<String> importFriends(string username)
    {
        List<String> buddyList = new List<String>();
        buddyList = _fbService.GetFriendIds().ToList<string>();
        return buddyList;
    }

    #endregion


    #region IDisposable Members

    public void Dispose()
    {
        _fbService.Dispose();
    }

    #endregion
}
